<?php
require('system-config.php');

function fetch_blogs($type) {
	global $wpdb;

	assert('deleted'==$type || 'active'==$type);

	$blog_ids = $wpdb->get_col("SELECT `id` FROM `$wpdb->blogs` WHERE `status` = '$type'");

	$blogs = array();

	if ($blog_ids) {
		$blog_ids = implode(',',$blog_ids);

		$blogs = $wpdb->get_results("
			SELECT `blog`, `option_value` AS `name`
			FROM $wpdb->options
			WHERE `option_name` = 'blogname'
			AND `blog` IN
				($blog_ids)
			ORDER BY `name`");
	}

	return $blogs;
}

function fetch_users(){
	global $wpdb;

	$users = $wpdb->get_results("
		SELECT user_login
		FROM ".$wpdb->users."
		WHERE user_admin = '0'
		ORDER BY user_login ASC;");

	$user_names = array();

	if ($users) {
		foreach ($users as $user) {
			$user_names[] = $user->user_login;
		}
	}

	return $user_names;
}

// ===========================================================================

$site_title = get_settings('site_title');

if ( isset($_POST['action']) ) {
	check_admin_referer();

	if(!('deleted'==$_POST['action'] || 'active' == $_POST['action'] || 'destroy' == $_POST['action']))
		die;

	validateToken($_POST['token'], 'blog-management.php', $_POST['action']);

	$whichblog = $_POST['whichblog'];
	$status = $_POST['action'];

	if('destroy' == $_POST['action']) {
		delete_blog(mysql_real_escape_string($whichblog));

	} else {
		$wpdb->query("UPDATE `$wpdb->blogs` SET `status`='".mysql_real_escape_string($status)."' WHERE `id`='".mysql_real_escape_string($whichblog)."'");

	}
}

$users = fetch_users();
$active_blogs = fetch_blogs('active');
$deactivated_blogs = fetch_blogs('deleted');

// ===========================================================================
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
	<title><?php echo $site_title ?>: Lyceum System Settings: Manage Blogs</title>

	<link rel="stylesheet" href="<?php echo LURL ?>/wp-admin/wp-admin.css" type="text/css" />
	<link rel="stylesheet" href="<?php echo LURL ?>/system-admin/lyceum.css" type="text/css" />

	<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php echo get_settings('blog_charset'); ?>" />
</head>


<body>

<div id="PageWrapper" class="AdminBlogs">

<?php require(ABSPATH . '/system-admin/lyceum-header.php'); ?>

	<hr />

	<!-- BEGIN: body -->
	<div id="BodyWrapper">

		<!-- BEGIN: main column -->
		<div id="MainColumn">

			<!-- BEGIN: main nav -->
			<ul id="MainNav">
				<li><span><a href="settings.php">System Settings</a></span></li>
				<li class="Selected"><span><a href="blog-management.php">Blogs</a></span></li>
				<li><span><a href="user-management.php">Users</a></span></li>
				<li><span><a href="systemplugins.php">Plugins</a></span></li>
				<li><span><a href="rewriterules.php">Apache Rewrite Rules</a></span></li>
			</ul>
			<div class="ClearFix TabBorder"><!-- do not remove --></div>
			<!-- END: main nav -->


			<div class="wrap">
				
				<!-- BEGIN: create a blog -->
				<form class="lyceum_form" name="create_blog_form" id="create_blog_form" method="post" action="createblog.php">
					<h2>Create a Blog</h2>
					<p>Create a new blog and assign an administrator.</p>

<?php
	if (count($users) > 0) {
?>
					<fieldset>
						<legend>Blog Creation</legend>

						<table class="FormTable" summary="">
							<col width="40%" />
							<col width="60%" />

							<tbody>
								<tr>
									<th scope="row"><label for="blogname">Blog name:</label></th>
									<td><input type="text" name="blogname" maxlength="100" class="TextInput" /></td>
								</tr>

								<tr>
									<th scope="row"><label for="admin">Blog administrator:</label></th>
									<td>
										<select name="admin" id="admin">
<?php
		foreach ($users as $user) {
?>
											<option value="<?php echo $user ?>"><?php echo $user ?></option>
<?php
		}
?>
										</select>
									</td>
								</tr>

							</tbody>
						</table>

						<?php echo tokeninput('createblog.php') ?>

						<div class="FormAction">
							<input type="submit" value="Create Blog" name="submit" />
						</div>

					</fieldset>
<?php
	} else {
?>
					<div class="Message">
						<strong>No users in database.</strong>
						<p>You must create users before you can create a blog.</p>
					</div>
<?php
	}
?>
				</form>
				<!-- END: create a blog -->

			</div>

			<div class="wrap">

				<!-- BEGIN: deactivate a blog -->
				<form class="lyceum_form" name="deactivate_blog_form" id="deactivate_blog_form" method="post" action="blog-management.php">
					<h2>Deactivate a Blog</h2>
					<p>Deactivate a blog from any use. This will appear as a "deletion" to the user, however all state and data is retained in the database.</p>

<?php
	if (count($active_blogs) > 0) {
?>
					<fieldset>
						<legend>Blog Deactivation</legend>

						<label for="whichblog_deactivate">Blog to deactivate:</label>
						<select name="whichblog" id="whichblog_deactivate">
							<option value="">Select a blog</option>
							<option value=""></option>
<?php
		foreach ($active_blogs as $blog) {
?>
							<option value="<?php echo $blog->blog ?>"><?php echo $blog->name ?></option>
<?php
		}
?>
						</select>

						<?php echo tokeninput('blog-management.php', 'deleted') ?>
						<input type="hidden" name="action" value="deleted" />

						<div class="FormAction">
							<input type="submit" value="Deactivate Blog" name="submit" />
						</div>
<?php
	} else {
?>
						<p><em>There are no active blogs at the moment.</em></p>
<?php
	}
?>
					</fieldset>
				</form>
				<!-- END: deactivate a blog -->

			</div>

			<div class="wrap">

				<!-- BEGIN: activate a blog -->
				<form class="lyceum_form" name="activate_blog_form" id="activate_blog_form" method="post" action="blog-management.php">
					<h2>Activate a Blog</h2>
					<p>Activate a deactivated blog for use.</p>

<?php
	if (count($deactivated_blogs) > 0) {
?>
					<fieldset>
						<legend>Blog Activation</legend>

						<label for="whichblog_activate">Blog to activate:</label>
						<select name="whichblog" id="whichblog_activate">
							<option value="">Select a blog</option>
							<option value=""></option>
<?php
		foreach ($deactivated_blogs as $blog) {
?>
							<option value="<?php echo $blog->blog ?>"><?php echo $blog->name ?></option>
<?php
		}
?>
						</select>

						<?php echo tokeninput('blog-management.php', 'active') ?>
						<input type="hidden" name="action" value="active" />

						<div class="FormAction">
							<input type="submit" value="Activate Blog" name="submit" />
						</div>

					</fieldset>
<?php
	} else {
?>
						<p><em>There are no deactivated blogs at the moment.</em></p>
<?php
	}
?>
				</form>
				<!-- END: activate a blog -->

			</div>

			<div class="wrap">

				<!-- BEGIN: delete a blog -->
				<form class="lyceum_form" name="delete_blog_form" id="delete_blog_form" method="post" action="blog-management.php">
					<h2>Delete a Blog</h2>
					<p>Completely delete a blog from the system. A blog must first be deactivated above for it to appear in this list.</p>

<?php
	if (count($deactivated_blogs) > 0) {
?>
					<fieldset>
						<legend>Blog Deletion</legend>

						<label for="whichblog_delete">Blog to delete:</label>
						<select name="whichblog" id="whichblog_delete">
							<option value="">Select a blog</option>
							<option value=""></option>
<?php
		foreach ($deactivated_blogs as $blog) {
?>
							<option value="<?php echo $blog->blog ?>"><?php echo $blog->name ?></option>
<?php
		}
?>
						</select>

						<?php echo tokeninput('blog-management.php', 'destroy') ?>
						<input type="hidden" name="action" value="destroy" />

						<div class="FormAction">
							<input type="submit" value="Delete Blog" name="submit" />
						</div>

					</fieldset>
<?php
	} else {
?>
						<p><em>There are no deactivated blogs at the moment.</em></p>
<?php
	}
?>
				</form>
				<!-- END: activate a blog -->

			</div>

		</div>
		<!-- END: main column -->

	</div>
	<!-- END: body -->

	<hr />

<?php include(ABSPATH . 'wp-admin/admin-footer.php'); ?>
		
</div>


</body>

</html>
